

<div class="main-content">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <ol class="breadcrumb">
                    <li>
                        <i class="clip-file"></i>
                        <a id="enviarcomprobante" href="#">
                            Ventas
                        </a>
                    </li>
                    <li class="active">
                        Facturación
                    </li>﻿  ﻿  ﻿  ﻿  ﻿  
                </ol>
                <div class="page-header">
                    <h1>Generar <small>  Comprobante</small></h1>
                </div>
            </div>
        </div>
 
        <div class="row">
            <div class="col-md-12">
                <div class="alert" id="mensajeC" style="display: none;"></div>
                <!-- start: DYNAMIC TABLE PANEL -->
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <i class="icon-external-link-sign"></i>
                        VENTA
                        <div class="panel-tools">
                            <a class="btn btn-xs btn-link panel-collapse collapses" href="#">
                            </a>
                            <a class="btn btn-xs btn-link panel-config" href="#panel-config" data-toggle="modal">
                                <i class="icon-wrench"></i>
                            </a>
                            <a class="btn btn-xs btn-link panel-refresh" href="#">
                                <i class="icon-refresh"></i>
                            </a>
                            <a class="btn btn-xs btn-link panel-expand" href="#">
                                <i class="icon-resize-full"></i>
                            </a>
                            <!--<a class="btn btn-xs btn-link panel-close" href="#">
                                <i class="icon-remove"></i>
                            </a> -->
                        </div>
                    </div>

                    <div class="panel-body">

                        <form action="" role="form" class="form-horizontal" id="frmComprobanteVenta" method="POST" >                            
                            <div class="row">

                                <div class="col-md-8"> 
                             
                                    <div class="form-horizontal">
                                             <div class="row">
                                            <div class="col-md-8">
                                                <div class="form-group">
                                                  <label class="col-sm-3 control-label" for="txtCanPro">
                                                        Tip. Comp
                                                    </label>
                                                    <div class="col-sm-7">                                                     
                                                        
                                                    <select class="form-control search-select" id="cmbTipDocumento" name="cmbTipDocumento">
                                                        <option value=""></option>
                                                        <option value="B">Boleta</option>
                                                        <option value="N">Nota de Venta</option>
                                                        <option value="F">Factura</option>                                                        

                                                    </select>
                                                
                                                    </div>  
                                                </div>  
                                            </div>                                        
                                            <div class="col-md-4">
                                            <div class="form-group">
                                                <label class="col-sm-1 control-label" for="txtSerieDic">
                                                    Serie
                                                </label>
                                                <div class="col-sm-4">
                                                    <input type="text" id="txtSerieDoc" value="0001" readonly="true"  class="form-control" name="txtSerieDoc">
                                                </div>
                                                <label class="col-sm-1 control-label" for="txtNumDoc">
                                                    N°
                                                </label>
                                                <div class="col-sm-5">
                                                    <input type="text" readonly="true" id="txtNumDoc"  class="form-control" name="txtNumDoc">
                                                </div>
                                            </div>
                                        </div>

                                        </div>  
                                        <div class="row">
                                            <div class="col-md-8">
                                                <div class="form-group">
                                                  <label class="col-sm-3 control-label" for="txtCanPro">
                                                        Cod Pedido
                                                    </label>
                                                    <div class="col-sm-7">
                                                      
                                                        <input type="text" id="descVenta" style="width: 250px" name="descVenta">
                                                    </div>  
                                                </div>  
                                            </div>                                        
                                           

                                        </div>                                    
                                    </div>
                                
                                    <div class="row" id="infocliente">
                                        <div class="col-md-8">

                                            <div class="form-group">
                                                <label class="col-sm-3 control-label" for="txtModPro">
                                                    Cliente
                                                </label>
                                                <div class="col-sm-9">                                            
                                                    <input type="text" id="descCli" disabled="true" class="form-control" name="descCli">
                                                </div>

                                            </div>
                                               

                                        </div>
                                         <div class="col-md-4">

                                            <div class="form-group">
                                                <label class="col-sm-3 control-label" for="txtModPro">
                                                    Nro.Doc
                                                </label>
                                                <div class="col-sm-9">                                            
                                                    <input type="text" id="nroDoc" disabled="true" class="form-control" name="nroDoc">
                                                </div>

                                            </div>
                                               

                                        </div>
                                           
                                     
                                    </div>

                                 
                                    <hr>   
                                    <div id="tablar">
                                      
                                            <div class="grid_5" id="contenidoCarrito" >
                                             <table class="table table-striped table-bordered table-condensed table-hover table-full-width" id="sample_2">
                            <thead>
                                <tr>
                                   
                                    <th style="width: 55%">Producto</th>
                                    <th style="width: 15%">Cantidad</th>
                                    <th style="width: 15%">P. Unitario</th>
                                    <th style="width: 15%">Sub Total</th>                             
                                   
                                </tr>
                            </thead>
                            <tbody id="tablaDet">
                                 				
                            </tbody>
                        </table>



                                            </div>
                                           <input type='hidden' id='txtValTotVent'  value='<?= $this->cart->total() ?> '  >
                                    </div>
                                </div>


                                <div class="col-md-4"> 
                                    <div class="row">
                                        <div class="form-group">
                                            <div class="col-md-11">
                                                <button type="button" class="btn btn-dark-grey btn-lg btn-block" style="color: springgreen;font-size: 65px;">
                                                    S./ <label id="lblVenta" >.00</label>  <i class="fa fa-arrow-circle-right"></i>
                                                </button>


                                            </div>
                                        </div>

                                    </div>
                                          
                                                
                                   

                                    <hr>
                                    <div class="row">

                                        <div class="form-group">
                                            <div class="col-md-12">
                                                <label class="col-sm-5 control-label" for="txtTelCli">
                                                    Valor de Venta
                                                </label>
                                                <div class="col-sm-6 input-group">
                                                    <span class="input-group-addon">
                                                        <i class="icon-dollar"></i>
                                                    </span>
                                                    <input type="text" id="lblValorVenta" name="lblValorVenta" style=" height:45px;  font-size: 25px;" readonly="true" class="form-control text-right" value="0.00">
                                                </div>
                                            </div>
                                        </div>

                                    </div>
                                    <div class="row">

                                        <div class="form-group">
                                            <div class="col-md-12">
                                                <label class="col-sm-5 control-label" for="txtTelCli">
                                                    I.G.V %
                                                </label>
                                                <div class="col-sm-6 input-group">
                                                    <span class="input-group-addon">
                                                        <i class="icon-dollar"></i>
                                                    </span>
                                                    <input type="text" id="lblValorIGV" name="lblValorIGV" style="height: 45px; font-size: 25px;" readonly="true" class="form-control text-right" value="0.00">
                                                </div>

                                            </div>
                                        </div>

                                    </div>
                                      
                                    <hr>
                                      <div class="row">
                                        <div class="form-group">
                                            <div class="col-md-12">
                                                <label class="col-sm-5 control-label" for="txtTelCli">
                                                    Forma de Pago
                                                </label>
                                                <div class="col-sm-6 input-group">
                                                    <select class="form-control search-select" id="cmbTipPago" name="cmbTipPago">
                                                        <option value=""></option>
                                                        <option value="E">Efectivo</option>
                                                        <option value="TC">Tarjeta de crédito</option>
                                                        <option value="C">Credito</option>
                                                        <option value="G">Giro</option>

                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div id="formapagotodo">
                                    <div id="formaPagoE" style="display: none;">
                                    <div class="row">

                                        <div class="form-group">
                                            <div class="col-md-12">
                                                <label class="col-sm-5 control-label" for="txtPago">
                                                    PAGÓ :
                                                </label>
                                                <div class="col-sm-6 input-group">
                                                    <span class="input-group-addon">
                                                        <i class="icon-dollar"></i>
                                                    </span>
                                                    <input type="text" id="txtPago"  style="font-size: 20px;"  class="form-control text-right" value="0.00">
                                                </div>

                                            </div>
                                        </div>

                                    </div>

                                    <div class="row">

                                        <div class="form-group">

                                            <div class="col-md-12">
                                                <label class="col-sm-5 control-label" for="txtVuelto">
                                                    VUELTO:
                                                </label>
                                                <div class="col-sm-6 input-group">
                                                    <span class="input-group-addon">
                                                        <i class="icon-dollar"></i>
                                                    </span>
                                                    <input type="text" id="txtVuelto"  style="font-size: 20px;" readonly="true" class="form-control text-right" value="0.00">
                                                </div>

                                            </div>
                                        </div>

                                    </div>
                                        </div>
                                    <div id="formaPagoC" style="display: none;">
                                 <div class="row">

                                        <div class="form-group">

                                            <div class="col-md-12">
                                                <label class="col-sm-5 control-label" for="txtVuelto">
                                                    PAGO A CUENTA:
                                                </label>
                                                <div class="col-sm-6 input-group">
                                                    <span class="input-group-addon">
                                                        <i class="icon-dollar"></i>
                                                    </span>
                                                    <input type="text" id="txtPagoCuenta" name="txtPagoCuenta" style="font-size: 20px;" class="form-control text-right" value="0.00">
                                                </div>

                                            </div>
                                        </div>

                                    </div>
                                  
                                    </div>
                                    <div id="Giro" style="display: none;">
                                         <div class="row">

                                        <div class="form-group">

                                            <div class="col-md-12">
                                                <label class="col-sm-5 control-label" for="txtNroDGiro">
                                                    Nro° Días:
                                                </label>
                                                <div class="col-sm-6 input-group">
                                                    <span class="input-group-addon">
                                                        <i class="icon-dashboard"></i>
                                                    </span>
                                                    <input type="text" id="txtNroDGiro" name="txtNroDGiro"  style="font-size: 20px;" class="form-control text-right" >
                                                </div>

                                            </div>
                                        </div>

                                    </div>   
                                    </div></div
                                    <!-- fin de forma de Pago  ---------->
                                    <hr>
                                   
                                <div class="col-md-12"></div>
                                <div class="col-md-6">
                                    <button class="btn btn-primary btn-block" type="button" id="guardarVenta" style="height: 48px;">
                                        <i class="icon-save"></i>
                                        Guardar                                        
                                    </button>
                                </div>
                                <div class="col-md-6">
                                    <button class="btn btn-danger btn-block" type="button" id="guardarProd" style="height: 48px;">
                                        <i class="icon-remove-sign"></i>
                                        Cancelar                                       
                                    </button>
                                </div>
                       
                                </div>
                            </div>

                      
  
                            <hr>

                        </form>  

                    </div>
                </div>
                <!-- end: DYNAMIC TABLE PANEL -->
            </div>

        </div>   

    </div>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
    function abrirreporte(url,id){
        window.open(url+"/"+id);
    }
     function generarCodigoVenta(tipo) {

                                                                $.ajax({
                                                                    type: "POST",
                                                                    url: "listCodVenta",
                                                                    data: {tipo: tipo},
                                                                    contentType: "application/x-www-form-urlencoded; charset=utf-8",
                                                                    success: function(msg) {
                                                                        $("#txtNumDoc").val(msg);
                                                                    }
                                                                });
                                                            }
      function calcVen(Total) {
                                                                $("#lblVenta").text(Total);
                                                                $("#lblValorIGV").val((Total * 18 / 100));
                                                                $("#lblValorVenta").val((Total - (Total * 18 / 100)));
                                                                if ($("#txtPago").val() === "0.00") {
                                                                    $("#txtVuelto").val("0.00");
                                                                } else {
                                                                     var vuelto = ($("#txtPago").val()) - ($("#lblVenta").text());
                                                                    $("#txtVuelto").val(vuelto + ".00");
                                                                }

                                                            }


  
 function verCliente(id) {
                                                        
                                                                $.ajax({
                                                                    type: "POST",
                                                                    url: "verCliente",
                                                                    data: {id: id}, //usarlo cuando se envía mediante POST
                                                                    contentType: "application/x-www-form-urlencoded; charset=utf-8",
                                                                    success: function(msg) {
                                                                        var elem = msg.split('|');                                                                         
                                                                        $("#descCli").val(elem[0]);
                                                                        $("#nroDoc").val(elem[1]);                                                         

                                                                       
                                                                    }
                                                                });
                                                               
                                                            }

 function verVenta(id) {
                                                        
                                                                $.ajax({
                                                                    type: "POST",
                                                                    url: "verventa",
                                                                    data: {id: id}, //usarlo cuando se envía mediante POST
                                                                    contentType: "application/x-www-form-urlencoded; charset=utf-8",
                                                                    dataType: 'json',
                                                                    success: function(msg) {
                                                                                  var tabla = "";
                                                                                  var suma=0;
                     
                $(msg).each(function() {
                if (this.registros !== null) {
                    for (var i = 0; i < this.registros.length; i++) {
                       suma +=  parseFloat(this.registros[i].detp_preciototal);
                        tabla += "<tr class='dato'>" +
                                "<td>" + this.registros[i].detp_nombreproducto + "</td>" +
                                "<td>" + this.registros[i].detp_cant + "</td>" +
                                "<td>" + this.registros[i].detp_preciounidad + "</td>" +
                                "<td>" + this.registros[i].detp_preciototal + "</td>" +
                                "</tr>";
                    }
                }else{
                tabla = "<tr><td colspan='4'>No hay registros</td></tr>";
                }
            });
            
                                                                        $("#tablaDet").html(tabla);
                                                                      calcVen(suma);
                                                                       
                                                                    }
                                                                });
                                                               
                                                            }
           $(function() {
           
                
          
           

           
       
           
           
                     $("#cmbTipDocumento").click(function() {
                                                                    generarCodigoVenta($('#cmbTipDocumento').select2("val"));
                                                                });
                               $("#guardarVenta").click(function()   {
                                    if ($('#descVenta').select2("val")==="") {
        alert("Valor introducido no válido");	}else{
           $.ajax({
                                                                    type: "POST",
                                                                    url: "IComprobantVenta",
                                                                    data: $("#frmComprobanteVenta").serialize(), //usarlo cuando se envía mediante POST
                                                                    contentType: "application/x-www-form-urlencoded; charset=utf-8",
                                                                    success: function(msg) {
                                                                 if(confirm("Desea imprimir el comprobante")){
        var ids=$('#descVenta').select2("val");
        abrirreporte('<?= base_url("ventas/reportes/imprimirComprobante"); ?>',ids);
                                                                      return true;
                                                                      }else{
                                                                     return false;    
                                                                      }
          
                                                                    }
                                                                });

                   }
                                   
                                    });

       $("#cmbTipPago").click(function() {
                                                                                                                                    
                                                                    if($('#cmbTipPago').select2("val")==="TC"){
                                                                         $("#formaPagoC").css("display","none");
                                                                          $("#Giro").css("display","none");
                                                                          $("#formaPagoE").css("display","none"); 
                                                                    }else if($('#cmbTipPago').select2("val")==="E"){
                                                                          $("#formaPagoC").css("display","none");
                                                                          $("#Giro").css("display","none");
                                                                          $("#formaPagoE").css("display","block"); 
                                                                    }else if($('#cmbTipPago').select2("val")==="C"){
                                                                          $("#formaPagoC").css("display","block");
                                                                          $("#Giro").css("display","none");
                                                                          $("#formaPagoE").css("display","none"); 
                                                                    }else if($('#cmbTipPago').select2("val")==="G"){
                                                                          $("#formaPagoC").css("display","none");
                                                                          $("#Giro").css("display","block");
                                                                          $("#formaPagoE").css("display","none"); 
                                                                    }else{                                                                         
                                                                          $("#formaPagoC").css("display","none");
                                                                          $("#Giro").css("display","none");
                                                                          $("#formaPagoE").css("display","none");                                                                         
                                                                    }
                                                                
                                                                   
                                                                });
                                                                    
                                                                /******  FILTRAR PRODUCTOS SELECT 2 VENTAS   **********/
                                                                $('#descVenta').select2({
                                                                    placeholder: 'Ingrese codigo Venta',
                                                                    minimumInputLength: 1,
                                                                    allowClear: true,
                                                                    ajax: {
                                                                        quietMillis: 150,
                                                                        url: 'filtradodeVenta',
                                                                        dataType: 'json',
                                                                        data: function(term, page) {
                                                                            return {
                                                                                apikey: "ju6z9mjyaj62d8uekgbvv26t",
                                                                                q: term
                                                                               
                                                                            };
                                                                        },
                                                                        results: function(data, page) {
                                                                            var myResults = [];
                                                                            $.each(data, function(index, item) {
                                                                                var total = item.length;
                                                                                var i;
                                                                                for (i = 0; i < total; i++) {
                                                                                    myResults.push({
                                                                                        id: item[i].pedi_pedido_id, //this.registros[index].cod,
                                                                                        text: item[i].codigo //this.registros[index].desc
                                                                                    });
                                                                                }
                                                                            });
                                                                            return {results: myResults};
                                                                        }
                                                                    }
                                                                }).on('change', function() {
                                                                    if($('#descVenta').select2("val")>0){
                                                                     verVenta($('#descVenta').select2("val"));
                                                                    verCliente($('#descVenta').select2("val"));
                                                                     
                                                                 }

                                                                }).trigger('change');

    });
</script>